<template>
  <div class="login">
    <!-- 左边登录模块 -->
    <div class="left">
      <!-- 标题栏 -->
      <div class="header">
        <img class="t1" src="@/assets/m_logo.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>

      <!-- 登录的表单模块 -->
      <el-form
        class="form"
        ref="form"
        :rules="rules"
        :model="form"
        label-width="0px"
      >
        <!-- 请输入手机号 -->
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="form.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- 请输入密码 -->
        <el-form-item prop="password">
          <el-input
            show-password
            prefix-icon="el-icon-lock"
            v-model="form.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 请输入验证码 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                prefix-icon="el-icon-key"
                v-model="form.code"
                placeholder="请输入验证码"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img @click="reCodeImg" class="code-img" :src="codeImgUrl" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 隐私条款 -->
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="into">登录</el-button>
          <div></div>
          <el-button type="primary" class="btn m26" @click="showRegister"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/login_banner_ele.png" alt />
    <register ref="register"></register>
  </div>
</template>

<script>
// 组件套用组件
import { saveLocal, getLocal } from "@/utils/local.js";
import { toLogin } from "@/api/login.js";
import register from "./register";
export default {
  components: {
    register,
  },
  data() {
    return {
      // baseUrl: "process.env.VUE_APP_URL + '/login'", //登录接口地址
      codeImgUrl: process.env.VUE_APP_URL + "/captcha?type=login", //验证码接口地址
      form: {
        phone: "", //是 string 手机号
        password: "", // 密码
        code: "", // 是 string 验证码
        isPass: [],
      },
      rules: {
        phone: [
          // 必填 非空验证，参数（form）为 空数组，''，undefiend 就不能通过验证
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 12, message: "长度为6到12个字符", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "长度为4个字符", trigger: "change" },
        ],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          // { min: 4, max: 4, message: "长度为4个字符", trigger: "change" },
        ],
      },
    };
  },
  created() {
    // if(已登录(有token)) {
    //   跳转至layout  this.$router.push('/layout')
    // }
    if (getLocal("token")) {
      this.$router.push("/layout");
    }
  },
  methods: {
    into() {
      this.$refs.form.validate((result) => {
        if (result) {
          // this.$message.success("验证通过");
          // 在表单验证成功后调用登录api方法
          toLogin(this.form).then((res) => {
            this.$message.success("登录成功");
            // 保存token
            saveLocal("token", res.data.data.token);
            // 登录成功后跳转到layout    跳转语法：this.$router.push('路径')
            this.$router.push("/layout");
            window.console.log("登录返回信息", res);
          });
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    // 刷新图形码,点击时给验证码地址随机加个数字或者时间戳
    reCodeImg() {
      this.codeImgUrl =
        process.env.VUE_APP_URL + "/captcha?type=login&cggh=" + Date.now();
    },
    // 注册事件
    showRegister() {
      // 父传子，1.在子组件标签定义一个ref  2.通过this.$refs获取值
      this.$refs.register.isShow = true;
    },
  },
};
</script>

<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  // 上下(垂直)居中
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 44px 42px;
    .header {
      display: flex;
      align-items: center;
      .t1 {
        width: 25px;
        height: 18px;
        margin-right: 14px;
      }
      .t2 {
        //   文字用内容撑开，所以不复制
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-right: 14px;
        margin-left: 14px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 14px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 31px;
      .code-img {
        width: 100%;
        height: 40px;
      }
      .btn {
        width: 100%;
      }
      .m26 {
        margin-top: 26px;
      }
    }
  }
}
</style>